﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.ServiceLocation;
using MyFramework.Common;
using MyFramework.Common.Attributes;
using MyFramework.Common.Interfaces;

namespace MyFramework.Core
{
    /// <summary>
    /// Module base
    /// </summary>
    [ModuleOrder(100)]
    public abstract class ModuleBase : IModule
    {
        /// <summary>
        /// Gets the width.
        /// </summary>
        /// <returns></returns>
        public abstract double GetWidth();

        /// <summary>
        /// Gets the heigth.
        /// </summary>
        /// <returns></returns>
        public abstract double GetHeigth();

        /// <summary>
        /// Inizializes the element.
        /// </summary>
        /// <typeparam name="C"></typeparam>
        /// <typeparam name="VM">The type of the m.</typeparam>
        /// <typeparam name="V"></typeparam>
        /// <param name="logicalName">Name of the logical.</param>
        /// <param name="titleWindows">The title windows.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        public virtual void InizializeElement<C, VM, V>(string logicalName, string titleWindows, IDictionary<string, object> initParameters = null)
            where C : IController
            where VM : IViewModel
            where V : IView
        {
            this.InizializeElement(typeof(C), typeof(VM), typeof(V), null, null, logicalName, titleWindows, initParameters);
        }

        /// <summary>
        /// Inizializes the element.
        /// </summary>
        /// <typeparam name="C"></typeparam>
        /// <typeparam name="VM">The type of the m.</typeparam>
        /// <typeparam name="V"></typeparam>
        /// <typeparam name="S"></typeparam>
        /// <typeparam name="M"></typeparam>
        /// <param name="logicalName">Name of the logical.</param>
        /// <param name="titleWindows">The title windows.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        public virtual void InizializeElement<C, VM, V, S, M>(string logicalName, string titleWindows, IDictionary<string, object> initParameters = null)
            where C : IController
            where VM : IViewModel
            where V : IView
            where S : ISettings
            where M : IModel
        {
            this.InizializeElement(typeof(C), typeof(VM), typeof(V), typeof(S), typeof(M), logicalName, titleWindows, initParameters);
        }

        /// <summary>
        /// Inizializes the element.
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="viewModel">The view model.</param>
        /// <param name="view">The view.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="model">The model.</param>
        /// <param name="logicalName">Name of the logical.</param>
        /// <param name="titleWindows">The title windows.</param>
        /// <param name="initParameters">The initialize parameters.</param>
        private void InizializeElement(Type controller, Type viewModel, Type view, Type settings, Type model, string logicalName, string titleWindows, IDictionary<string, object> initParameters = null)
        {
            IController currentController = null;
            var factory = ServiceLocator.Current.GetInstance<IControllerFactory>(CommonUnityName.UNITY_FACTORY);
            factory.CreateController(controller, initParameters, (c) =>
            {
                currentController = c;
            }, null, (e) => { });
            if (model != null)
            {
                IModel lModel = Activator.CreateInstance(model) as IModel;
            }
            if (settings != null)
            {
                ISettings lSettings = Activator.CreateInstance(settings) as ISettings;
            }

        }

        #region IModule Members

        /// <summary>
        /// Notifies the module that it has be initialized.
        /// </summary>
        public abstract void Initialize();


        #endregion
    }
}
